package com.song.shop.quartz;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: Song Yinhui
 * @Date: 2018/11/26 17:22
 */
@Component
public class QuartzTest3 {


    Boolean flag = false;

    @Scheduled(cron = "0 34 11 * * *")
    public void run() throws InterruptedException {
        if (!getLock()) {
            flag = true;
            System.out.println("quartzTest3 ----- " + new Date() + Thread.currentThread().getName());
            Thread.sleep(3000);
            relaseLock();
        }

    }

    @Scheduled(cron = "0 34 11 * * *")
    public void run12() throws InterruptedException {

        if (!getLock()) {
            flag = true;
            System.out.println("quartzTest22 ----- " + new Date() + Thread.currentThread().getName());
            Thread.sleep(3000 );
            relaseLock();
        }

    }


    private synchronized Boolean getLock() {
        return flag;
    }


    private synchronized void relaseLock() {
        this.flag = false;
    }

    //@Scheduled(fixedDelay = 10 * 1000)  //fixedDelay延迟10s
    public void run1() {
        System.out.println("fixedDelay ----" + new Date());
    }
}
